package cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataMake;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet.Reduce;
import cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet.concept;

public class dataMake {

	/**
	 * @param args
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public static HashSet<String> getAllClass(ArrayList<Integer> documents)
			throws ClassNotFoundException, SQLException {
		Connection con = DBConnectionFactory.getConnection();//.getFalconConnection();
		HashSet<String> hs = new HashSet<String>();
		int typeid = 0;
		try {
			typeid = URIIDCache.getInstance().getURIID(
					"http://www.w3.org/1999/02/22-rdf-syntax-ns#type", false);
		} catch (Throwable e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		for (int i = 0; i < documents.size(); ++i) {
			String query = "SELECT distinct o FROM quadruple where p = \"u"+typeid+"\" and doc_id = "
					+ documents.get(i);
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(query);
			while (rs.next()) {
				String conceptid = rs.getString(1).toString().substring(1);
				// System.out.println(rs.getString(1) + " " + i);
				hs.add(conceptid);
			}
			rs.close();
			st.close();
		}
		con.close();
		return hs;
	}

	public static ArrayList<concept> getAllClassInfo(
			ArrayList<Integer> documents) throws ClassNotFoundException,
			SQLException {
		ArrayList<concept> info = new ArrayList<concept>();
		Object ob[] = getAllClass(documents).toArray();
		Connection con = DBConnectionFactory.getConnection();//.getFalconConnection();
		for (int i = 0; i < ob.length; ++i) {
			// System.out.println(ob[i]);
			String id = (String) ob[i];
			concept c = new concept();
			c.id = id;
			c.label = "";
			String query = "SELECT uri FROM uri where id = " + id;

			// System.out.println(ob[i] + " " + query);
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(query);
			while (rs.next()) {
				c.uri = rs.getString(1);
				c.label = Reduce.uri(rs.getString(1));
				// System.out.println(c.label);
				// System.out.println(c.label);
				info.add(c);
			}
			rs.close();
			st.close();
		}
		con.close();
		return info;
	}

}
